local skynet = require "skynet"
require "skynet.manager"
local identity = ...

local base = require "util.base"
local class = require "util.class"
local producermgr = class(base)

function producermgr:ctor()
	self.running = true
	self.i = 1
end

function producermgr:task()
	while true do
		local answer = skynet.call(".store", "lua", "have_vacancy")
		if answer then
			skynet.error(identity, "produce:", identity..self.i)
			skynet.send(".store", "lua", "produce", identity..self.i)
			self.i = self.i+1
		else
			skynet.error("producer:", identity, "stoptask")
			self.running = false
			break
		end
	end
end

function producermgr:restart()
	if not self.running then
		self.running = true
		self:task()
	end
end

skynet.start(function()	
	skynet.send(".store", "lua", "phandler", skynet.self())
	local mgr = producermgr.new()
	
	skynet.dispatch("lua", function(session, address, sign, ...)
		mgr:run(sign, ...)
	end)

	skynet.fork(function() mgr:task() end)
	skynet.error("producer start")
end)